//
//  StockDetailDCell.m
//  MyStock
//
//  Created by 刘翔 on 2024/12/24.
//

#import "StockDetailDCell.h"
@interface StockDetailDCell()<WKNavigationDelegate,WKScriptMessageHandler>

@property(nonatomic, strong)WKWebView *webView;
@end

@implementation StockDetailDCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setModel:(CandidateStock *)model{
    _model = model;
    self.lowPriceL.textColor = Color_gree;
    self.lowPriceL.text = [NSString stringWithFormat:@"%.2f",[model.low doubleValue]];
    self.price1L.text = [NSString stringWithFormat:@"%.2f",[model.NowCost15 doubleValue]];
    self.price2L.text = [NSString stringWithFormat:@"%.2f",[model.NowCost30 doubleValue]];
    self.nameL.text = model.Name;
    self.codeL.text = model.Code;
    self.diffL.code = model.Code;
    
    __weak typeof(self) weakSelf = self;
    self.diffL.stockChangeHandle = ^(StockModel * _Nullable stockData) {
        
        if(model.Code == stockData.scode){
            float diff = ([stockData.last_px floatValue] - [weakSelf.model.NowCost30 floatValue]) / [weakSelf.model.NowCost30 floatValue] * 100;
            weakSelf.diffL.text = [NSString stringWithFormat:@"%.2f",diff];
            if (diff > 0) {
                weakSelf.diffL.textColor = Color_gree;
            }else if (diff == 0){
                weakSelf.diffL.textColor = Color_3;
            }else{
                weakSelf.diffL.textColor = Color_red;
            }
        }
        
        
    };
    
    NSMutableString *urlStr = [NSMutableString stringWithFormat:@"%@code=%@",WEBURL,model.Code];
    if([model.NowCost15 floatValue] > 0){
        [urlStr appendFormat:@"&line15=%@",model.NowCost15];
    }
    if([model.NowCost30 floatValue] > 0){
        [urlStr appendFormat:@"&line30=%@",model.NowCost30];
    }
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
    [self.webView loadRequest:request];
    
    
}

- (void)setSeletedStock:(SeletedStock *)seletedStock{
    _seletedStock = seletedStock;
    self.lowPriceL.textColor = Color_gree;
    self.lowPriceL.text = [NSString stringWithFormat:@"--"];
    self.price1L.text = [NSString stringWithFormat:@"%.2f",[seletedStock.CostLine15 doubleValue]];
    self.price2L.text = [NSString stringWithFormat:@"%.2f",[seletedStock.CostLine30 doubleValue]];
    self.nameL.text = seletedStock.name;
    self.codeL.text = seletedStock.code;
    self.diffL.code = seletedStock.code;
    
    __weak typeof(self) weakSelf = self;
    self.diffL.stockChangeHandle = ^(StockModel * _Nullable stockData) {
        
        if(seletedStock.code == stockData.scode){
            float diff = ([stockData.last_px floatValue] - [weakSelf.seletedStock.CostLine30 floatValue]) / [weakSelf.seletedStock.CostLine30 floatValue] * 100;
            weakSelf.diffL.text = [NSString stringWithFormat:@"%.2f",diff];
            if (diff > 0) {
                weakSelf.diffL.textColor = Color_gree;
            }else if (diff == 0){
                weakSelf.diffL.textColor = Color_3;
            }else{
                weakSelf.diffL.textColor = Color_red;
            }
        }
        
        
    };
    
    NSMutableString *urlStr = [NSMutableString stringWithFormat:@"%@code=%@",WEBURL,seletedStock.code];
    if([seletedStock.CostLine15 floatValue] > 0){
        [urlStr appendFormat:@"&line15=%@",seletedStock.CostLine15];
    }
    if([seletedStock.CostLine30 floatValue] > 0){
        [urlStr appendFormat:@"&line30=%@",seletedStock.CostLine30];
    }
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
    [self.webView loadRequest:request];
    
}

- (void)setPositionModel:(PositionModel *)positionModel{
    _positionModel = positionModel;
    
    self.priceName1L.text = @"防守线1:";
    self.priceName2L.text = @"防守线2:";

    self.lowPriceL.textColor = Color_gree;
    self.lowPriceL.text = [NSString stringWithFormat:@"--"];
    self.price1L.text = [NSString stringWithFormat:@"%.2f",[positionModel.defensive15 doubleValue]];
    self.price2L.text = [NSString stringWithFormat:@"%.2f",[positionModel.defensive30 doubleValue]];
    self.nameL.text = positionModel.name;
    self.codeL.text = positionModel.code;
    self.diffL.code = positionModel.code;
    
    __weak typeof(self) weakSelf = self;
    self.diffL.stockChangeHandle = ^(StockModel * _Nullable stockData) {
        
        if(positionModel.code == stockData.scode){
            float diff = ([stockData.last_px floatValue] - [weakSelf.positionModel.nowCost30 floatValue]) / [weakSelf.positionModel.nowCost30 floatValue] * 100;
            weakSelf.diffL.text = [NSString stringWithFormat:@"%.2f",diff];
            if (diff > 0) {
                weakSelf.diffL.textColor = Color_gree;
            }else if (diff == 0){
                weakSelf.diffL.textColor = Color_3;
            }else{
                weakSelf.diffL.textColor = Color_red;
            }
        }        
    };
    
    NSMutableString *urlStr = [NSMutableString stringWithFormat:@"%@code=%@",WEBURL,positionModel.code];
    if([positionModel.defensive15 floatValue] > 0){
        [urlStr appendFormat:@"&line15=%@",positionModel.defensive15];
    }
    if([positionModel.defensive30 floatValue] > 0){
        [urlStr appendFormat:@"&line30=%@",positionModel.defensive30];
    }
    [urlStr appendFormat:@"&color=%@",@"d81e06"];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
    [self.webView loadRequest:request];
}

- (WKWebView *)webView{
    if (_webView == nil) {
        __weak typeof(self) weakSelf = self;
        WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
        [config.userContentController addScriptMessageHandler:self name:@"nativeHandler"];
        _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, HWScreenW, HWScreenH) configuration:config];
        _webView.backgroundColor = [UIColor whiteColor];
        _webView.navigationDelegate = weakSelf;
        _webView.scrollView.bounces = NO;
        [self.contentView addSubview:_webView];
        [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(weakSelf.headerView.mas_bottom);
                    make.left.right.bottom.equalTo(weakSelf.contentView);
        }];
        
        
    }
    return _webView;
}

#pragma WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
    if ([message.name isEqualToString:@"nativeHandler"]) {
        NSLog(@"vue===%@",message.body);
    }
}


@end
